﻿/* 
 *  <copyright file="AssetGameService.cs" company="Wesley Jowitt">
 *  
 *  SobrietyEngine: Copyright (c) 2011 Wesley Jowitt
 *  
 *  This software is distributed under a software license.
 *   * As of the creation time of this file, the license in use is the Microsoft Permissive License (Ms-PL) 
 *    * The license can be viewed at:
 *     * License.Txt in the root folder of this project
 *      * http://sobrietyengine.codeplex.com/license
 *       * http://www.microsoft.com/opensource/licenses.mspx
 *       
 *  </copyright>
 *   */

using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;

namespace SobrietyEngine.Assets
{
    public class AssetGameService : GameComponent, SobrietyEngine.Assets.IAssetGameService
    {
        Dictionary<String, ContentManager> contentManagers = new Dictionary<string,ContentManager>();
        List<ContentManager> managerList = new List<ContentManager>();
        
        public AssetGameService(Game game)
            : base(game)        
        {
            game.Services.AddService(typeof(IAssetGameService), this);
        }

        public void CreateContentManager(String name, String path)
        {
            contentManagers.Add(name, new ContentManager(Game.Content.ServiceProvider, path));
            managerList.Add(contentManagers[name]);
        }

        public void AddContentManager(String name, ContentManager cm)
        {
            contentManagers.Add(name, cm);
            managerList.Add(cm);
        }

        public ContentManager GetContentManager(String name)
        {
            return contentManagers[name];
        }

        public T Load<T>(String assetName)
        {
            //changed to use a list of managers, reerse order. this is because
            //the first CM's added are likely to be the default CM for SobrietyEngine
            //the users is last added, so chances are most of our resources come from that, so we try it first
            for(int i= managerList.Count -1; i >=0; --i)
            {
                try
                {
                    T o = managerList[i].Load<T>(assetName);
                        return o;
                }
                catch(Exception ex)
                {
                    //we likely tried to load from the wrong content manager
                    //continue on our way
                    Logging.Logger.Debug("AssetGameService::Exception caught (likely tried wrong CM) " + ex.Message);
                    continue;
                }
            }
            Logging.Logger.Error("Failed to load content: " + assetName);
            return default(T);
        }

        public T Load<T>(String contentManagerName, String assetName)
        {
            ContentManager cm = contentManagers[contentManagerName];            
            T o = cm.Load<T>(assetName);
            return o;
        }
    }
}
